export default {
  install(Vue, Options) {
    /***
     * 防抖 单位时间只触发最后一次
     *  @param {Function} fn - 执行事件
     *  @param {?Number|500} time - 间隔时间
     *  @param {?String|"click"} event - 事件类型 例："click"
     *  @param {Array} binding.value - [fn,time,event]
     *  例：<el-button v-debounce="[reset,500,`click`]">刷新</el-button>
     *  也可简写成：<el-button v-debounce="[reset]">刷新</el-button>
     */
    Vue.directive('debounce', {
      inserted: function (el, binding) {
        let [fn, time = 500, event = 'click'] = binding.value;
        let timer;
        el.addEventListener(event, () => {
          timer && clearTimeout(timer);
          timer = setTimeout(() => fn(), time);
        });
      },
    });
  },
};
